
package gui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Model;
import model.NotifyPicassa;

/**
 * @author Troy Ferrell
 *
 */
public class ExpressionPanel extends JPanel
{
	private Model myModel;
	
	private PicassaObservable myObservable;
	
	private JTextField myExpressionField;
	
	public ExpressionPanel(Model newModel)
	{
		setModel(newModel);
		
		myObservable = new PicassaObservable();
		
		addExpressionComponents();
	}
	
	/**
	 * Create input components for accepting expressions from user
	 * 
	 * @return JPanel that holds expression components
	 */
	private void addExpressionComponents()
	{
		JLabel expLabel = new JLabel("Enter Expression:");
		
		myExpressionField = new JTextField();
		myExpressionField.setPreferredSize(new Dimension(500, 25));
		myExpressionField.addKeyListener(new KeyListener()
        {
            public void keyPressed (KeyEvent e) 
            {
            	if( e.getKeyCode() == KeyEvent.VK_ENTER)
            		processExpression();	
            }
            public void keyReleased (KeyEvent e) {}
            public void keyTyped (KeyEvent e) {}
        });
		
		JButton enterButton = new JButton("Enter");
		enterButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				processExpression();
			}
		});
		
		this.add(expLabel);
		this.add(myExpressionField);
		this.add(enterButton);
	}
	
	public void processExpression()
	{
		NotifyPicassa.showNotification("Processing expression...");
		
		myObservable.setState(true);
		myObservable.notifyObservers(myExpressionField.getText());
	}
	
	public void addExpressionObserver(Observer ob)
	{
		if(ob != null)
			myObservable.addObserver(ob);
	}
	
	public void setModel(Model newModel)
	{
		if(newModel != null)
			myModel = newModel;
	}
}
